package com.ruoyi.system.monitortask.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.GlobalException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.V2.goodInfo.mapper.GoodsInfoMapper;
import com.ruoyi.system.monitortask.domain.MonitorTask;
import com.ruoyi.system.monitortask.domain.MonitorTaskGoods;
import com.ruoyi.system.monitortask.mapper.MonitorTaskGoodsMapper;
import com.ruoyi.system.monitortask.service.IMonitorTaskGoodsService;
import com.ruoyi.system.monitortask.service.IMonitorTaskService;
import com.ruoyi.system.monitortask.vo.MonitorTaskGoodsVo;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 账号监控任务关联商品表(monitor_task_goods)表服务实现类
 *
 * @author zyy
 * @date 2024-06-21 15:56:49
 */
@Service("monitorTaskGoodsService")
public class MonitorTaskGoodsServiceImpl extends ServiceImpl<MonitorTaskGoodsMapper, MonitorTaskGoods> implements IMonitorTaskGoodsService {


    @Autowired
    private IMonitorTaskService monitorTaskService;
    @Autowired
    private MonitorTaskGoodsMapper monitorTaskGoodsMapper;
    @Autowired
    private GoodsInfoMapper goodsInfoMapper;

    @Override
    @Transactional
    public boolean addBatch(MonitorTaskGoods monitorTaskGoods) {
        if (StringUtils.isEmpty(monitorTaskGoods.getGoodsIds())) {
            throw new GlobalException("商品id不能为空");
        }
        MonitorTask monitorTask = monitorTaskService.getById(monitorTaskGoods.getMonitorTaskId());
        if (Objects.isNull(monitorTask)) {
            throw new GlobalException("未找到此分组");
        }
        List<MonitorTaskGoods> monitorTaskGoodsList = new ArrayList<>();
        String[] split = monitorTaskGoods.getGoodsIds().split(",");
        List<Long> goodsIdList = new ArrayList<>();
        MonitorTaskGoods monitorTaskGoodsInsert = null;
        Long userId = SecurityUtils.getUserId();
        for (String s : split) {
            monitorTaskGoodsInsert = new MonitorTaskGoods();
            monitorTaskGoodsInsert.setCreateId(userId);
            monitorTaskGoodsInsert.setMonitorTaskId(monitorTaskGoods.getMonitorTaskId());
            Long goodsId = Long.valueOf(s);
            monitorTaskGoodsInsert.setGoodsId(goodsId);
            goodsIdList.add(goodsId);
            monitorTaskGoodsList.add(monitorTaskGoodsInsert);
        }
        String state = null;
        if (monitorTask.getType().intValue() == 1) {
            state = "1";
        } else if (monitorTask.getType().intValue() == 2) {
            state = "2";
        }
        saveBatch(monitorTaskGoodsList);
        goodsInfoMapper.updateMonitoringState(goodsIdList, state);
        return true;
    }

    @Override
    @Transactional
    public boolean deleteBatch(Long[] ids) {
        List<Long> idList = Arrays.asList(ids);
        LambdaQueryWrapper<MonitorTaskGoods> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.select(MonitorTaskGoods::getGoodsId);
        queryWrapper.in(MonitorTaskGoods::getId, idList);
        List<MonitorTaskGoods> list = list(queryWrapper);
        if (CollectionUtils.isNotEmpty(list)) {
            List<Long> goodsIdList = list.stream().map(MonitorTaskGoods::getGoodsId).collect(Collectors.toList());
            goodsInfoMapper.updateMonitoringState(goodsIdList, "3");
        }
        removeByIds(idList);
        return true;
    }

    @Override
    public List<MonitorTaskGoodsVo> getByMonitorTaskId(Long monitorTaskId) {
        MonitorTask monitorTask = monitorTaskService.getById(monitorTaskId);
        if (Objects.isNull(monitorTask)) {
            throw new GlobalException("未找到此分组");
        }
        List<MonitorTaskGoodsVo> monitorTaskGoodsVos = null;
        if (Objects.equals(monitorTask.getType().intValue(), 1)) {
            monitorTaskGoodsVos = monitorTaskGoodsMapper.getMonitorTaskGoodsListDB(monitorTaskId);
        } else {
            monitorTaskGoodsVos = monitorTaskGoodsMapper.getMonitorTaskGoodsListZS(monitorTaskId);
        }
        return monitorTaskGoodsVos;
    }
}

